home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9663 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Handling complex numbers...
  5. Date: 12 Mar 1996 09:25:52 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i4c30INNs95@keats.ugrad.cs.ubc.ca>
  8. References: <4hi113$2i8k@mercury.cc.uottawa.ca> <larry_kearney-0803960747220001@amaryllisp1.appsig.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <larry_kearney-0803960747220001@amaryllisp1.appsig.com>,
  12. Larry Kearney <larry_kearney@appsig.com> wrote:
  13.  >> Dear fellow netters,
  14.  >> 
  15.  >> Can someone kindly explain to me how to represent complex numbers in C?
  16.  >> I am writing a numerical method program to calculate the area under
  17.  >> a curve using Simpson's Rule.  The problem that I face right now is
  18.  >> the representation of "i" (where i^2 = -1) in my C program.
  19.  >> 
  20.  >> Here's a simple example of what I mean:
  21.  >>   
  22.  >>   _b
  23.  >>  |           
  24.  >>  | exp(ix) dx 
  25.  >> _|             
  26.  >> a
  27.  >> 
  28.  >> 
  29.  >> How do I implement this function exp(ix)?
  30.  >> 
  31.  >> I'm grateful for your help.
  32.  >> 
  33.  >> Charles Tran
  34.  >> -- 
  35.  >> Charles
  36.  >
  37.  >The C language is not terribly efficient when it comes to complex numbers.
  38.  
  39. Correction: your code below is not terribly efficient. You can use macros to
  40. represent common complex operations. These can be as good as C++ inline
  41. member functions, for instance.
  42.  
  43.  >You can represent a complex number as a struct, i.e.,
  44.  >
  45.  >typedef struct
  46.  >{
  47.  >   double re;  /* real part of the complex number */
  48.  >   double im;  /* imaginary part of the complex number */
  49.  >} complex;
  50.  
  51.  >but the big problem is that the arithmetic operators don't know what to do
  52.  >when presented with values that are structures. As a result, the
  53.  >programmer is required to implement functions that perform that standard
  54.  >operations and that take complex structs as arguments. For example,
  55.  >
  56.  >void AddComplex ( complex *a, complex *b, complex *c )
  57.  >{
  58.  >   c->real = a->real + b->real;
  59.  >   c->imag = a->imag + b->imag;
  60.  >}
  61.  >
  62.  >This makes coding an expressions such as
  63.  >
  64.  >   x = 2.0 * ( y + z ) / d     /* all variables are complex */
  65.  >
  66.  >an exercise is calling functions and the creation of lots of temporary
  67.  >complex arguments.
  68.  
  69. Many quality C compilers will inline such small functions, provided they have
  70. internal linkage.
  71.  
  72. You have never used pre-processor macros to do inline code?  Here is a
  73. two-operand form of the addition, though you could do a C <- A + B
  74. three-operand form this way as well:
  75.  
  76. #define AddComplex(A,B) ((A)->real += (B)->real, (A)->imag += (B)->imag)
  77.  
  78. Here, a comma expression is used to carry out two assignment expressions left
  79. to right. The ``return value'' of the macro is the imaginary part of the
  80. result (i.e. the result of the rightmost expression).
  81.  
  82.  >Alternate
  83.  >solutions would be to attempt to code your function using either Fortran, where
  84.  >complex arithmetic is built-in (does anybody know that anymore) or code it
  85.  >in C++ where you can easily create a complex class and then write function
  86.  >that overrides the normal arithmetic operators and trig functions when
  87.  >complex arithmetic is used. This approach would allow a more natural
  88.  >implementation of expressions.
  89.  
  90. C++ operator redefinition is just ``syntactic sugar'' (in the words of the
  91. comp.lang.c++ FAQ) to hide a function call (which can be inlined, of course).
  92. It's not any more efficient than the above macros, though arguably more
  93. elegant.
  94. -- 
  95.  
  96.